{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Kinetic modeling – sinusoidal temperature\n",
    "In this notebook we will look at the solution of a kinetic problem where the temperature varies sinusoidally."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "from collections import defaultdict\n",
    "from chempy import ReactionSystem\n",
    "from chempy.kinetics.ode import get_odesys\n",
    "from chempy.kinetics.rates import SinTemp\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rsys = ReactionSystem.from_string(\"\"\"\n",
    "2 HNO2 -> H2O + NO + NO2; EyringParam(dH=85e3, dS=10)\n",
    "2 NO2 -> N2O4; EyringParam(dH=70e3, dS=20)\n",
    "\"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "st = SinTemp(unique_keys='Tbase Tamp Tangvel Tphase'.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "odesys, extra = get_odesys(rsys, include_params=False, substitutions={'temperature': st})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "init_conc = defaultdict(lambda: 0, HNO2=1, H2O=55)\n",
    "params = dict(\n",
    "    Tbase=300,\n",
    "    Tamp=10,\n",
    "    Tangvel=2*math.pi/10,\n",
    "    Tphase=-math.pi/2\n",
    ")\n",
    "duration = 60"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def integrate_and_plot(system):\n",
    "    result = system.integrate(duration, init_conc, params, integrator='cvode', nsteps=2000)\n",
    "    result.plot(names='NO HNO2 N2O4 NO2'.split(), title_info=2)\n",
    "    print({k: v for k, v in sorted(result.info.items()) if not k.startswith('internal')})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "integrate_and_plot(odesys)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since we are using [pyodesys](https://github.com/bjodah/pyodesys) we can reformulate our ODE-system as an autonomous one. This can help for stiff systems since time will then be explicitly represented in the Jacobian matrix."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "autsys = odesys.as_autonomous()\n",
    "assert len(autsys.dep) == len(odesys.dep) + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "autsys.exprs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "integrate_and_plot(autsys)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
